package com.sanyue.designPatterns;

/**
 * @author sanyue
 * @version 1.0
 * @description: TODO-双重校验锁实现对象单例
 * @date 2022/2/5 上午9:57
 */
public class Singleton {

/*  1.为 uniqueInstance 分配内存空间
    2.初始化 uniqueInstance
    3.将 uniqueInstance 指向分配的内存地址
    */
    //volatile避免指令重排
    private volatile static Singleton uniqueInstance;

    private Singleton(){

    }

    public static Singleton getUniqueInstance(){
        if (uniqueInstance == null){
            synchronized(Singleton.class){
                if (uniqueInstance == null){
                    uniqueInstance = new Singleton();
                }
            }
        }
        return uniqueInstance;
    }
}
